<template>
  <div class="app-container" id="introProduct">
    <dialogForm class="sel" @update="getProductList" @close="close" v-if="showDialog"></dialogForm>
    <el-button type="primary" icon="el-icon-plus" @click="showDialog=!showDialog" class="addButton">添加</el-button>
    <div class="sel">
      <span>请选择商家：</span>
      <el-select v-model="companyId" placeholder="请选择标签组" @change="getProductList">
        <el-option
          v-for="item in options"
          :key="item.companyId"
          :label="item.companyName"
          :value="item.companyId">
        </el-option>
      </el-select>
    </div>
    <el-table :data="list" v-loading.body="listLoading" element-loading-text="Loading" border fit highlight-current-row
              max-height="550" class="ProductTable">
      <el-table-column label="投资类型" align="center" width="100">
        <template slot-scope="scope">
          {{scope.row.investType == '0' ? '首投' : '复投'}}
        </template>
      </el-table-column>
      <el-table-column label="起投金额" align="center" width="100">
        <template slot-scope="scope">
          <span>{{scope.row.subscriptionOrigin}}</span>
        </template>
      </el-table-column>
      <el-table-column label="投资期限" align="center" width="100">
        <template slot-scope="scope">
          <span>{{scope.row.term}}</span>
        </template>
      </el-table-column>
      <el-table-column label="返利金额" align="center" width="100">
        <template slot-scope="scope">
          <span>{{scope.row.profit}}</span>
        </template>
      </el-table-column>
      <el-table-column label="综合利率" align="center" width="100">
        <template slot-scope="scope">
          <span>{{scope.row.allInRate}}</span>
        </template>
      </el-table-column>
      <el-table-column label="超级返金额" align="center" width="100">
        <template slot-scope="scope">
          <span>{{scope.row.extraProfit}}</span>
        </template>
      </el-table-column>
      <el-table-column label="超级返时间" align="center">
        <template slot-scope="scope">
          <span
            v-show="scope.row.extraBeginDate != null">{{dateFormat(scope.row.extraBeginDate)}}~{{dateFormat(scope.row.extraEndDate)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="上下架状态" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.saleStatus === 0 ? '未上架' : scope.row.saleStatus === 1 ? '上架中' : '已下架'}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="操作" width="250" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" @click="changeSale(scope)">{{scope.row.saleStatus !== 1 ? '上架' : '下架'}}</el-button>
          <el-button type="primary" size="small" @click="edit(scope)">修改</el-button>
          <el-button type="danger" size="small" @click="deleted(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="curPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="size">
      </el-pagination>
    </div>
    <div class="buttonBox" v-if="route.name=='addBusiness'">
      <div class="button" @click="goTo('label')">上一步</div>
      <div class="button" @click="goTo('rebate')">下一步</div>
    </div>
  </div>
</template>

<script>
  import moment from 'moment'
  import { companySelectList } from 'api/business'
  import { productList, productDelete, productSale } from 'api/product'
  import dialogForm from '../components/dialog'
  export default {
    components: {
      dialogForm
    },
    data() {
      return {
        list: [],
        listLoading: true,
        options: [],
        companyId: '',
        size: 0,
        pageSize: 10,
        curPage: 1,
        showDialog: false
      }
    },
    created() {
      this.getBusinessList()
    },
    beforeDestroy() {
      sessionStorage.removeItem('businessId')
    },
    computed: {
      route() {
        return this.$route
      }
    },
    watch: {
      companyId(val, oldval) {
        sessionStorage.setItem('businessId', val)
      }
    },
    methods: {
      dateFormat(parm) {
        return moment(new Date(parm)).format('YYYY-MM-DD')
      },
      async changeSale(scope) {
        let saleStatus = 0
        if (scope.row.approve !== 1) {
          this.$alert('该产品尚未通过审核', '提示')
          return
        }
        if (scope.row.saleStatus !== 1) {
          saleStatus = 1
        } else {
          saleStatus = 2
        }
        console.log(scope.row)
        const parm = {
          productId: scope.row.id,
          seleType: saleStatus
        }
        console.log(parm)
        try {
          let back = await productSale(parm)
          back = back.data
          if (back.resultCode === '200') {
            this.getProductList()
          } else {
            this.$alert(back.msg, '提示')
          }
        } catch (err) {
          this.$alert('出错了' + err, '提示')
        }
        console.log(saleStatus)
      },
      // 获取商家列表
      async getBusinessList() {
        try {
          let back = await companySelectList()
          back = back.data
          if (back.resultCode === '200') {
            this.options = back.result
            this.companyId = back.result[0].companyId
            this.getProductList()
          }
        } catch (err) {
          this.$alert('请求出错', '提示')
        }
      },
      // 获取列表
      async getProductList() {
        this.close()
        const parm = {
          pageSize: this.pageSize,
          curPage: this.curPage - 1,
          productType: 0,
          companyId: this.companyId,
          investType: ''
        }
        console.log(parm)
        try {
          let back = await productList(parm)
          back = back.data
          if (back.resultCode === '200') {
            this.list = back.result
            this.size = back.size
            this.listLoading = false
          } else {
            this.$alert(back.msg, '提示')
          }
        } catch (err) {
          this.$alert('请求出错', '提示')
        }
      },
      // 删除
      async deleted(scope) {
        try {
          let back = await productDelete(scope.row.id)
          back = back.data
          if (back.resultCode === '200') {
            console.log(back)
            this.getProductList()
          } else {
            this.$alert(back.msg, '提示')
          }
        } catch (err) {
          alert('请求出错', '提示')
        }
      },
      edit(scope) {
        sessionStorage.setItem('productId', scope.row.id)
        this.showDialog = true
      },
      close() {
        sessionStorage.removeItem('productId')
        this.showDialog = false
      },
      goTo(parm) {
        this.$emit('goTo', parm)
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.currentPage = 1
        this.getProductList()
      },
      handleCurrentChange(val) {
        this.curPage = val
        this.getProductList()
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .ProductTable {
    margin-top: 80px;
  }

  .buttonBox {
    overflow: hidden;
    .button {
      float: left;
      width: 200px;
      height: 40px;
      line-height: 40px;
      border-radius: 5px;
      margin: 40px;
      color: white;
      background: #409EFF;
      text-align: center;
      cursor: pointer;
    }
  }

  .sel {
    float: left;
    &:nth-child(2) {
      margin-left: 20px;
    }
  }

  .block {
    float: right;
    margin-top: 10px;
  }

  .addButton {
    float: left;
  }
</style>
